package com.cspg.ssss.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.domain.*;
import com.cspg.ssss.service.*;
import com.cspg.ssss.vo.DepartVo;
import com.cspg.ssss.vo.PageInfoVo;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/grade")
public class ProblemController {
    private static Logger log = LoggerFactory.getLogger(ProblemController.class.getName());

    @Autowired
    ProblemService problemService;

    @Autowired
    ApproveService approveService;

    @Autowired
    FeedbackService feedbackService;

    @Autowired
    WxDepartmentService wxDepartmentService;

    @Autowired
    ObjHistoryService objHistoryService;

    @Autowired
    ApproveUserSelectService approveUserSelectService;

    @Autowired
    CheckBatchService checkBatchService;

    @Autowired
    GradeRoleUserService gradeRoleUserService;

    @Autowired
    CheckItemService checkItemService;

    @Autowired
    WxSendMsgService wxSendMsgService;

    @Autowired
    DaiBanService daiBanService;

    @GetMapping("/sync-problem")
    public ResultVo syncProblemFromSrc(){
        String strResult = problemService.SycProblemFromDyffAndDfyy_ch();
        ResultVo retVo = null;
        if(strResult.equals("")){
            retVo = ResultVoUtil.success("success",strResult);
        }else {
            retVo = ResultVoUtil.error("9999",strResult);
        }

        return retVo;
    }

    @GetMapping("/getProblemList")
    public ResultVo getProblemList(@RequestParam(required = false) Map<String,String> req){
        String opType = req.get("op");
        String userid = req.get("userid");
        List<ProblemInst> retlist = new ArrayList<ProblemInst>();
        if(opType.equalsIgnoreCase("grade")){
            retlist = problemService.getProblemListByGradeUser(userid,0);
        }

        if(opType.equalsIgnoreCase("approve")){
            retlist = problemService.getProblemListByApproveAndStatus(userid,1);
        }

        if(opType.equalsIgnoreCase("view_grade")){
            retlist = problemService.getProblemListByViewerAndStatus(userid,2);
        }

        if(opType.equalsIgnoreCase("feedback")){

        }

        if(opType.equalsIgnoreCase("feedback_approve")){
            retlist = problemService.getProblemListByFeebackApproveAndStatus(userid,3);
        }

        if(opType.equalsIgnoreCase("feedback_recheck")){
            retlist = problemService.getProblemListByViewerAndStatus(userid,4);
        }

        ResultVo retVo = ResultVoUtil.success("success",retlist);
        return retVo;
    }

/*    @GetMapping("/getProblemDetail")
    public ResultVo getProblemDetail(String pid){
        JSONObject problemObj = new JSONObject();
        ProblemInst probleminst = problemService.getProblemInstById(pid);
        List<GradeApproveEntity> approve_list = null;

        problemObj.put("problem_inst",probleminst);

        Map<String,GradeApproveEntity> retMap = approveService.getApprovedList(pid);

        problemObj.put("grade_approve",retMap.get("grade_approve"));
        problemObj.put("feedback_approve",retMap.get("feedback_approve"));
        problemObj.put("feedback_recheck",retMap.get("feedback_recheck"));

        ProblemFeedbackEntity fbEn = feedbackService.getProblemFeedbackEntity(pid);
        problemObj.put("feedback",fbEn);


        ResultVo retVo = ResultVoUtil.success("success",problemObj);
        return retVo;
    }*/

/*    @PostMapping("/updateProblemInst")
    public ResultVo updateProblemInst(@RequestBody JSONObject obj){

        String op = obj.getString("op");
        JSONObject probleminst = obj.getJSONObject("problem_inst");
        JSONObject opcontent = obj.getJSONObject("opcontent");
        if(op.equalsIgnoreCase("grade")){
            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("check_item",probleminst.getString("check_item"));
            updatefiled.put("score",probleminst.getString("score"));
            updatefiled.put("check_userid",probleminst.getString("check_userid"));
            updatefiled.put("check_username",probleminst.getString("check_username"));
            updatefiled.put("problem_status",1);
            updatefiled.put("grade_time",new Date());

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("grade_approve")){
            GradeApproveEntity en = new GradeApproveEntity();
            en.setApprove_type(0);
            en.setApprove_time(new Date());
            en.setApprove_userid(opcontent.getString("approve_userid"));
            en.setApprove_username(opcontent.getString("approve_username"));
            en.setApprove_result(Integer.parseInt(opcontent.getString("approve_result")));
            en.setProblem_id(opcontent.getString("problem_id"));
            en.setRefuse_reason(opcontent.getString("refuse_reason"));

            approveService.updateApprove(en);
            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 2;
            if(approve_result == 0)
                status_result = 0;
            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("feedback")){
            ProblemFeedbackEntity en = new ProblemFeedbackEntity();
            en.setFeedback_reason(opcontent.getString("feedback_reason"));
            en.setFeedback_time(new Date());
            en.setFeedback_userid(opcontent.getString("approve_userid"));
            en.setFeedback_username(opcontent.getString("approve_username"));
            en.setProblem_id(opcontent.getString("problem_id"));

            feedbackService.updateProblemFeedback(en);


            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",3);

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("feedback_approve")){
            GradeApproveEntity en = new GradeApproveEntity();
            en.setApprove_type(1);
            en.setApprove_time(new Date());
            en.setApprove_userid(opcontent.getString("approve_userid"));
            en.setApprove_username(opcontent.getString("approve_username"));
            en.setApprove_result(Integer.parseInt(opcontent.getString("approve_result")));
            en.setProblem_id(opcontent.getString("problem_id"));
            en.setRefuse_reason(opcontent.getString("refuse_reason"));

            approveService.updateApprove(en);
            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 4;
            if(approve_result == 0)
                status_result = 2;

            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("feedback_recheck")){
            GradeApproveEntity en = new GradeApproveEntity();
            en.setApprove_type(2);
            en.setApprove_time(new Date());
            en.setApprove_userid(opcontent.getString("approve_userid"));
            en.setApprove_username(opcontent.getString("approve_username"));
            en.setApprove_result(Integer.parseInt(opcontent.getString("approve_result")));
            en.setProblem_id(opcontent.getString("problem_id"));
            en.setRefuse_reason(opcontent.getString("refuse_reason"));

            approveService.updateApprove(en);

            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 0;
            if(approve_result == 0)
                status_result = 2;

            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);

            problemService.updateProblemInst(updatefiled);
        }

        ResultVo retVo = ResultVoUtil.success("success",null);
        return retVo;
    }*/

    @PostMapping("/changeAssessmentDept")
    public ResultVo changeAssessmentDept(@RequestBody JSONObject obj){
        HashMap<String,Object> updatefiled = new HashMap<String, Object>();
        updatefiled.put("id",obj.getString("id"));
        String assessment_dept = obj.getString("assessment_dept");
        //assessment_dept = assessment_dept;
        updatefiled.put("assessment_dept",assessment_dept);

        problemService.updateProblemInst(updatefiled);

        ResultVo retVo = ResultVoUtil.success("success",null);
        return retVo;
    }

    /**
     * 获取打分问题列表
     * @param obj
     * @return
     */
    @PostMapping("/getProblemListByCondition")
    public ResultVo getProblemListByCondition(@RequestBody JSONObject obj){

        PageInfoVo<ProblemInst> retPageVo = new PageInfoVo<ProblemInst>();

        String strPgnum = obj.getString("pageNum");
        String strPgsize = obj.getString("pageSize");
        String user_id = obj.getString("user_id");
        String op = obj.getString("op");
        String strProblemStatus = obj.getString("problem_status");
        String strProblemType = obj.getString("problem_type");
        String matters = obj.getString("matters");
        String assessmentDept = obj.getString("assessment_dept");
        String user_dept = obj.getString("department_path");
        String batchDept = obj.getString("batchDept");
        String scope = obj.getString("scope");

        int pgnum = Integer.parseInt(strPgnum);
        int pgsize = Integer.parseInt(strPgsize);
        int ptype = Integer.parseInt(strProblemType);

        //根据考核载体编号获取考核载体描述内容
        if(matters !=null && !matters.isEmpty()){
            List<CheckItem> cls = checkItemService.getCheckCategroy();
            for(int k=0;k<cls.size();k++){
                if(cls.get(k).getId().equals(matters)){
                    matters = cls.get(k).getItem();
                    break;
                }
            }
        }

        CheckBatchInst batchInst = checkBatchService.getCurrentCheckBachInst(ptype);
        if(batchInst == null){
            ResultVo retVo = ResultVoUtil.success("无批次",retPageVo);
            return retVo;
        }

        HashMap<String,Object> conditionfiled = new HashMap<String, Object>();
        conditionfiled.put("problem_type",ptype);
        conditionfiled.put("batch_id",batchInst.getId());

        if(strProblemStatus != null && (!strProblemStatus.isEmpty())){
            int problem_stauts = Integer.parseInt(strProblemStatus);
            conditionfiled.put("problem_status",problem_stauts);
        }

        if(matters != null && (!matters.isEmpty())){
            conditionfiled.put("matters",matters);
        }

        //根据当前人userid和op获取部门过滤条件
        if(op.equals("grade")){//打分部门查看

            List<String> departsls = new ArrayList<String>();
            departsls.add(user_dept);
            conditionfiled.put("assessment_dept",departsls);

            if(batchInst.getCheck_type() == 0 && scope.equals("1")){
                DepartVo vo = wxDepartmentService.getUserSubDept(user_id,user_dept);
                conditionfiled.put("grade_dept",vo.getDepartment_path());
            }

            PageInfo<ProblemInst> retLs = problemService.getProblemListByPage(pgnum,pgsize,conditionfiled);

            retPageVo.setPageNum(retLs.getPageNum());
            retPageVo.setPageSize(retLs.getPageSize());
            retPageVo.setTotal(retLs.getTotal());
            retPageVo.setList(retLs.getList());

        }
        if(op.equals("check")){//打分审核
            HashMap<String,Object> approvecondition = new HashMap<String, Object>();
            approvecondition.put("user_id",user_id);
            approvecondition.put("problem_type",ptype);
            approvecondition.put("batch_id",batchInst.getId());
            approvecondition.put("approve_type",0);
            approvecondition.put("grade_dept",user_dept);

            if(!batchDept.equals("")) {
                if (ptype == 0) {
                    approvecondition.put("company", batchDept);
                } else {
                    approvecondition.put("dept", batchDept);
                }
            }

            if(strProblemStatus != null && (!strProblemStatus.isEmpty())){
                int problem_stauts = Integer.parseInt(strProblemStatus);
                approvecondition.put("problem_status",problem_stauts);
            }

            if(matters != null && (!matters.isEmpty())){
                approvecondition.put("matters",matters);
            }

            PageInfo<ProblemInst> retLs = problemService.getProblemListWithApproveByPage(pgnum,pgsize,approvecondition);

            retPageVo.setPageNum(retLs.getPageNum());
            retPageVo.setPageSize(retLs.getPageSize());
            retPageVo.setTotal(retLs.getTotal());
            retPageVo.setList(retLs.getList());

        }
        if(op.equals("feedback") || op.equals("feedback_modify")){//被考核部门查看
            if(batchInst.getCurrent_stage() > 0){
                List<String> departsls = new ArrayList<String>();
                departsls.add(user_dept);

                if(ptype == 0){//公司对单位

                    conditionfiled.put("company_like",departsls);
                }
                if(ptype == 1){//单位对部门
                    conditionfiled.put("dept_like",departsls);
                }

                if(conditionfiled.get("problem_status") == null && op.equals("feedback_modify")){
                    conditionfiled.put("problem_status",6);
                }

                PageInfo<ProblemInst> retLs = problemService.getProblemListByPage(pgnum,pgsize,conditionfiled);

                retPageVo.setPageNum(retLs.getPageNum());
                retPageVo.setPageSize(retLs.getPageSize());
                retPageVo.setTotal(retLs.getTotal());
                retPageVo.setList(retLs.getList());
            }
        }

        if(op.equals("approve")){//反馈审核
            if(batchInst.getCurrent_stage() > 0) {
                HashMap<String, Object> approvecondition = new HashMap<String, Object>();
                approvecondition.put("user_id", user_id);
                approvecondition.put("problem_type", ptype);
                approvecondition.put("approve_type", 1);
                approvecondition.put("batch_id",batchInst.getId());
                if (strProblemStatus != null && (!strProblemStatus.isEmpty())) {
                    int problem_stauts = Integer.parseInt(strProblemStatus);
                    approvecondition.put("problem_status", problem_stauts);
                }

                if (matters != null && (!matters.isEmpty())) {
                    approvecondition.put("matters", matters);
                }
                PageInfo<ProblemInst> retLs = problemService.getProblemListWithApproveByPage(pgnum, pgsize, approvecondition);

                retPageVo.setPageNum(retLs.getPageNum());
                retPageVo.setPageSize(retLs.getPageSize());
                retPageVo.setTotal(retLs.getTotal());
                retPageVo.setList(retLs.getList());
            }

        }

        if(op.equals("approve_review")){//局领导反馈审核（针对公司对单位打分问题）
            if(batchInst.getCurrent_stage() > 0) {
                HashMap<String, Object> approvecondition = new HashMap<String, Object>();
                approvecondition.put("user_id", user_id);
                approvecondition.put("problem_type", ptype);
                approvecondition.put("approve_type", 5);
                approvecondition.put("batch_id",batchInst.getId());
                if (strProblemStatus != null && (!strProblemStatus.isEmpty())) {
                    int problem_stauts = Integer.parseInt(strProblemStatus);
                    approvecondition.put("problem_status", problem_stauts);
                }

                if (matters != null && (!matters.isEmpty())) {
                    approvecondition.put("matters", matters);
                }
                PageInfo<ProblemInst> retLs = problemService.getProblemListWithApproveByPage(pgnum, pgsize, approvecondition);

                retPageVo.setPageNum(retLs.getPageNum());
                retPageVo.setPageSize(retLs.getPageSize());
                retPageVo.setTotal(retLs.getTotal());
                retPageVo.setList(retLs.getList());
            }

        }

        if(op.equals("review")){//反馈复核
            if(batchInst.getCurrent_stage() > 1) {
                HashMap<String, Object> approvecondition = new HashMap<String, Object>();
                approvecondition.put("user_id", user_id);
                approvecondition.put("problem_type", ptype);
                approvecondition.put("approve_type", 2);
                approvecondition.put("batch_id",batchInst.getId());

                if (strProblemStatus != null && (!strProblemStatus.isEmpty())) {
                    int problem_stauts = Integer.parseInt(strProblemStatus);
                    approvecondition.put("problem_status", problem_stauts);
                }

                if (matters != null && (!matters.isEmpty())) {
                    approvecondition.put("matters", matters);
                }

                PageInfo<ProblemInst> retLs = problemService.getProblemListWithApproveByPage(pgnum, pgsize, approvecondition);

                retPageVo.setPageNum(retLs.getPageNum());
                retPageVo.setPageSize(retLs.getPageSize());
                retPageVo.setTotal(retLs.getTotal());
                retPageVo.setList(retLs.getList());
            }

        }

        if(retPageVo.getList() == null){
            retPageVo.setList(new ArrayList<ProblemInst>());
        }

        JSONObject retObj = (JSONObject)JSONObject.toJSON(retPageVo);


        int totalCnt = -1;
        int noGradeCnt = -1;
        //只有评分列表才真实查询打分情况统计
        if(op.equals("grade")) {
            Map<String,Object> statCondition = new HashMap<String, Object>();
            statCondition.put("assessment_dept",user_dept);
            statCondition.put("batch_id",batchInst.getId());
            totalCnt = problemService.getProblemCountByStatusWithCondition(statCondition);
            statCondition.put("problem_status", 0);
            noGradeCnt = problemService.getProblemCountByStatusWithCondition(statCondition);
        }

        JSONObject statObj = new JSONObject();
        statObj.put("total",totalCnt);
        statObj.put("nograde",noGradeCnt);
        retObj.put("stat",statObj);

        ResultVo retVo = ResultVoUtil.success("success",retObj);
        return retVo;
    }

    @GetMapping("/getProblemDetail")
    public ResultVo getProblemDetail(String pid){
        JSONObject problemObj = new JSONObject();
        ProblemInst probleminst = problemService.getProblemInstDetail(pid);
        List<GradeApproveEntity> approve_list = null;

        //将ProblemInst实例中的batch_id作为当前阶段状态返回，便于前端判断当前阶段
        CheckBatchInst batchInst = checkBatchService.getCurrentCheckBachInst(probleminst.getProblem_type());
        probleminst.setBatch_id(batchInst.getCurrent_stage());

        problemObj.put("problem_inst",probleminst);

        ProblemFeedbackEntity fbEn = feedbackService.getProblemFeedbackEntity(pid);
        problemObj.put("feedback",fbEn);

        Map<String,Object> historyCondition = new HashMap<String,Object>();
        historyCondition.put("obj_id",pid);
        List<ObjHistoryEntity> historyLs = objHistoryService.getObjectHistory(historyCondition);
        problemObj.put("history",historyLs);

        ResultVo retVo = ResultVoUtil.success("success",problemObj);
        return retVo;
    }

    /**
     * 打分问题流程流转
     * @param obj
     * @return
     */
    @PostMapping("/updateProblemInst")
    public ResultVo updateProblemInst(@RequestBody JSONObject obj){

        String op = obj.getString("op");
        JSONObject probleminst = obj.getJSONObject("problem_inst");
        JSONObject opcontent = obj.getJSONObject("opcontent");
        if(op.equalsIgnoreCase("save")){//打分
            //更新问题数据
            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("check_item",probleminst.getString("check_item"));
            updatefiled.put("score",probleminst.getString("score"));
            updatefiled.put("check_userid",probleminst.getString("check_userid"));
            updatefiled.put("check_username",probleminst.getString("check_username"));

            updatefiled.put("grade_time",new Date());

             updatefiled.put("problem_status",8);

            String dept = probleminst.getString("assessment_dept");
            int chk_type = Integer.parseInt(probleminst.getString("problem_type"));
            if(chk_type == 0) {
                String userid = probleminst.getString("check_userid");

                DepartVo subdept = wxDepartmentService.getUserSubDept(userid, dept);
                updatefiled.put("grade_dept",subdept.getDepartment_path());
            }else {
                updatefiled.put("grade_dept",dept);
            }
            problemService.updateProblemInst(updatefiled);

        }
        if(op.equalsIgnoreCase("grade")){//统一提交审核

        }
        if(op.equalsIgnoreCase("check")){//打分审核

            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 2;
            if(approve_result == 0){
                status_result = 0;
                //发送打分审核拒绝通知
            }else {
                //查询关联批次，如果当前批次状态为2，则为反馈重新打分，打分后，直接关闭问题
                CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(probleminst.getString("problem_type")));
                if(binst != null && binst.getCurrent_stage() == 2){
                    status_result = 7;
                }
            }

            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);
            if(approve_result == 1){
                String strScore = opcontent.getString("new_score");
                updatefiled.put("score",strScore);
            }

            problemService.updateProblemInst(updatefiled);
            //problemService.setNextStage(Integer.parseInt(probleminst.getString("problem_type")));
        }
        if(op.equalsIgnoreCase("feedback")){
            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));

            if(opcontent.getString("approve_result").equals("1")){
                updatefiled.put("problem_status",7);
            }else{
                ProblemFeedbackEntity en = new ProblemFeedbackEntity();
                en.setFeedback_reason(opcontent.getString("feedback_reason"));
                en.setFeedback_time(new Date());
                en.setFeedback_userid(opcontent.getString("approve_userid"));
                en.setFeedback_username(opcontent.getString("approve_username"));
                en.setProblem_id(opcontent.getString("problem_id"));
                en.setFeedback_result(0);
                en.setFeedback_score(opcontent.getString("feedback_score"));

                feedbackService.updateProblemFeedback(en);
                updatefiled.put("problem_status",3);
                //发送反馈审核通知
            }
            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("approve")){

            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 4;
            if(approve_result == 0){
                status_result = 6;//反馈待修改
                //发送反馈待修改通知
            }else {
                if(probleminst.getString("problem_type").equals("0")){
                    status_result = 4;
                    //发送局领导审核通知
                }else {
                    status_result = 5;

                }
            }
            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("approve_review")){

            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));
            int status_result = 5;
            if(approve_result == 0) {
                status_result = 6;
                //发送反馈待修改通知
            }

            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));
            updatefiled.put("problem_status",status_result);

            problemService.updateProblemInst(updatefiled);
        }
        if(op.equalsIgnoreCase("review")){

            ProblemFeedbackEntity fen = feedbackService.getProblemFeedbackEntity(probleminst.getString("id"));
            int approve_result = Integer.parseInt(opcontent.getString("approve_result"));

            HashMap<String,Object> updatefiled = new HashMap<String, Object>();
            updatefiled.put("id",probleminst.getString("id"));


            int status_result = 0;
            if(approve_result == 0){
                status_result = 7;
                fen.setFeedback_result(0);
                updatefiled.put("problem_status",status_result);
                //发送反馈拒绝通知

            }else {
                status_result = 0;
                fen.setFeedback_result(1);
                updatefiled.put("problem_status",status_result);
                //updatefiled.put("score",fen.getFeedback_score());
                //发送反馈审核通过通知
            }

            feedbackService.updateProblemFeedback(fen);

            problemService.updateProblemInst(updatefiled);
        }
        //记录审核人
        addApproveUsers(op,opcontent,probleminst);
        //记录历史
        addOpHistory(op,opcontent,probleminst);

        //发送消息
        sendImMessage(op,opcontent,probleminst);
        ResultVo retVo = ResultVoUtil.success("success",null);
        return retVo;
    }

    private void addApproveUsers(String op,JSONObject opObj,JSONObject problemObj){
        JSONArray arr = opObj.getJSONArray("receivers");

        if(arr == null || arr.isEmpty()){
            return;
        }
        int approve_type = 0;
        List<String> ids = new ArrayList<String>();
        for(int i=0;i<arr.size();i++){
            ids.add(arr.getJSONObject(i).getString("user_id"));
        }
        if(op.equals("grade")){
            approve_type = 0;
            //wxSendMsgService.sendCheckTipInfo(ids,Integer.parseInt(problemObj.getString("problem_type")));//打分审核通知
        }
        if(op.equals("feedback")){
            approve_type = 1;
        }
        if(problemObj.getString("problem_type").equals("0") && op.equals("approve")){
            approve_type = 5;

        }
        if(problemObj.getString("problem_type").equals("1") && op.equals("approve")){
            approve_type = 2;
        }
        if(op.equals("approve_review")){
            approve_type = 2;
        }

        Map<String,Object> condition = new HashMap<String,Object>();
        condition.put("problem_id",problemObj.getString("id"));
        condition.put("approve_type",approve_type);
        approveUserSelectService.deleteApproveUserSelect(condition);

        for(int i=0;i<arr.size();i++){
            ApproveUserSelectEntity en = new ApproveUserSelectEntity();
            en.setProblem_id(problemObj.getString("id"));
            en.setUser_id(arr.getJSONObject(i).getString("user_id"));
            en.setUser_name(arr.getJSONObject(i).getString("user_name"));

            en.setApprove_type(approve_type);
            approveUserSelectService.addApproveUserSelect(en);
        }
    }

    private void addOpHistory(String op,JSONObject opObj,JSONObject problemObj){
        ObjHistoryEntity hisEn = new ObjHistoryEntity();
        hisEn.setObj_id(problemObj.getString("id"));
        hisEn.setCreate_time(new Date());
        hisEn.setObj_type(0);

        if(op.equals("save")){
            hisEn.setOp_event(op);
            //查询关联批次，如果当前批次状态为2，则为反馈重新打分，打分后，直接关闭问题
            CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(problemObj.getString("problem_type")));
            if(binst != null && binst.getCurrent_stage() == 2){
                hisEn.setOp_content("最终评分-评分保存，总计扣分"+problemObj.getString("score"));
            }else {
                hisEn.setOp_content("评分保存，总计扣分"+problemObj.getString("score"));
            }

            hisEn.setOp_userid(problemObj.getString("check_userid"));
            hisEn.setOp_username(problemObj.getString("check_username"));
        }

        if(op.equals("grade")){
            hisEn.setOp_event(op);
            //查询关联批次，如果当前批次状态为2，则为反馈重新打分，打分后，直接关闭问题
            CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(problemObj.getString("problem_type")));
            if(binst != null && binst.getCurrent_stage() == 2){
                hisEn.setOp_content("最终评分，总计扣分"+problemObj.getString("score"));
            }else {
                hisEn.setOp_content("评分提交，总计扣分"+problemObj.getString("score"));
            }


            //hisEn.setOp_content("打分，总计扣分"+problemObj.getString("score"));
            hisEn.setOp_userid(problemObj.getString("check_userid"));
            hisEn.setOp_username(problemObj.getString("check_username"));
        }
        if(op.equals("check")){
            hisEn.setOp_event(op);
            String opcontent = "评分审核";
            CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(problemObj.getString("problem_type")));
            if(binst.getCurrent_stage() == 0) {
                if (opObj.getString("approve_result").equals("1")) {
                    opcontent += ",审核通过";
                }
                if (opObj.getString("approve_result").equals("0")) {
                    opcontent += ",审核拒绝。拒绝原因：" + opObj.getString("refuse_reason");
                }
            }
            if(binst.getCurrent_stage() == 2){
                opcontent = "最终评分审核";
                if (opObj.getString("approve_result").equals("1")) {
                    opcontent += ",审核通过，问题结束";
                }
                if (opObj.getString("approve_result").equals("0")) {
                    opcontent += ",审核拒绝。拒绝原因：" + opObj.getString("refuse_reason");
                }
            }
            hisEn.setOp_content(opcontent);
            hisEn.setOp_userid(opObj.getString("approve_userid"));
            hisEn.setOp_username(opObj.getString("approve_username"));
        }
        if(op.equals("feedback")){
            hisEn.setOp_event(op);
            String opcontent = "评分反馈";
            if(opObj.getString("approve_result").equals("1")){
                opcontent = ",无异议";
            }
            if(opObj.getString("approve_result").equals("0")){
                opcontent += "，反馈理由："+opObj.getString("feedback_reason")+"申请修改扣分数为："+opObj.getString("feedback_score");
            }
            hisEn.setOp_content(opcontent);
            hisEn.setOp_userid(opObj.getString("approve_userid"));
            hisEn.setOp_username(opObj.getString("approve_username"));
        }
        if(op.equals("approve")){
            hisEn.setOp_event(op);
            String opcontent = "问题反馈审核";
            if(opObj.getString("approve_result").equals("1")){
                opcontent += ",审核通过";
            }
            if(opObj.getString("approve_result").equals("0")){
                opcontent += ",审核拒绝。拒绝原因："+opObj.getString("refuse_reason");
            }
            hisEn.setOp_content(opcontent);
            hisEn.setOp_userid(opObj.getString("approve_userid"));
            hisEn.setOp_username(opObj.getString("approve_username"));
        }
        if(op.equals("approve_review")){
            hisEn.setOp_event(op);
            String opcontent = "局领导反馈审核";
            if(opObj.getString("approve_result").equals("1")){
                opcontent += ",审核通过";
            }
            if(opObj.getString("approve_result").equals("0")){
                opcontent += ",审核拒绝。拒绝原因："+opObj.getString("refuse_reason");
            }
            hisEn.setOp_content(opcontent);
            hisEn.setOp_userid(opObj.getString("approve_userid"));
            hisEn.setOp_username(opObj.getString("approve_username"));
        }
        if(op.equals("review")){
            hisEn.setOp_event(op);
            String opcontent = "反馈终审";
            if(opObj.getString("approve_result").equals("1")){
                opcontent += ",审核通过。审核意见："+ opObj.getString("refuse_reason")+"，反馈部门申请扣分："+opObj.getString("feedback_score");
            }
            if(opObj.getString("approve_result").equals("0")){
                opcontent += ",审核拒绝。拒绝原因："+opObj.getString("refuse_reason");
            }
            hisEn.setOp_content(opcontent);
            hisEn.setOp_userid(opObj.getString("approve_userid"));
            hisEn.setOp_username(opObj.getString("approve_username"));
        }

        objHistoryService.addObjectHistory(hisEn);
    }

    /**
     * 将部门路径由->分隔改成/分隔，加上超高压前缀
     *
     * @param deptPath
     * @return
     */
    private String ConvertDeptToPath(String deptPath) {
        String newpath = deptPath.replaceAll("->", "/");
        newpath = "超高压输电公司/" + newpath;
        return newpath;
    }

    /**
     * 打分流转过程中发送即使消息提醒
     * @param op
     * @param opObj
     * @param problemObj
     */
    private void sendImMessage(String op,JSONObject opObj,JSONObject problemObj){

        try {
            int check_type = Integer.parseInt(problemObj.getString("problem_type"));
            JSONArray arr = opObj.getJSONArray("receivers");

            List<String> ids = new ArrayList<String>();
            StringBuilder idList = new StringBuilder();
            String bmmc = "";

            if (arr != null && (!arr.isEmpty())) {
                for (int i = 0; i < arr.size(); i++) {
                    ids.add(arr.getJSONObject(i).getString("user_id"));
                    idList.append(";").append(arr.getJSONObject(i).getString("user_id"));
                    bmmc = arr.getJSONObject(i).getString("department_displaypath");
                }
            }

//        if(op.equals("grade")){
//            if(ids.size() > 0){
//                wxSendMsgService.sendCheckTipInfo(ids,check_type);
//            }
//        }

            BacklogEntity updateBacklog = new BacklogEntity();
            if (op.equals("save")) {//打分保存
                updateBacklog.setDbstatus("8");
            }
            if (op.equals("check")) {
                //回退重新打分
                int approve_result = Integer.parseInt(opObj.getString("approve_result"));
                if (approve_result == 0) {
                    String uid = problemObj.getString("check_userid");
                    List<String> tmp = new ArrayList<>();
                    tmp.add(uid);
                    wxSendMsgService.sendRejectTip(tmp, check_type);
                    //修改待办状态
                    updateBacklog.setDbstatus("0");
                    updateBacklog.setBmmc(problemObj.getString("khbm"));
                    updateBacklog.setDbman("-1");
                } else {
                    //修改待办状态
                    updateBacklog.setDbstatus("2");
                    updateBacklog.setBmmc(ConvertDeptToPath(problemObj.getString("bmmc")));
                    updateBacklog.setDbman("-1");
                }

            }
            if (op.equals("feedback")) {
                wxSendMsgService.feedbackGradeTipInfo(ids, check_type);
                int approve_result = Integer.parseInt(opObj.getString("approve_result"));
                if (approve_result == 1) {
                    updateBacklog.setDbstatus("-100");
                } else {
                    //修改待办状态
                    updateBacklog.setDbstatus("3");
                    updateBacklog.setBmmc(bmmc);
                    updateBacklog.setDbman(idList.toString());
                }
            }
            if (op.equals("approve")) {
                if (opObj.getString("approve_result").equals("0")) {
                    ProblemFeedbackEntity fbEn = feedbackService.getProblemFeedbackEntity(problemObj.getString("id"));
                    List<String> tmp = new ArrayList<String>();
                    tmp.add(fbEn.getFeedback_userid());
                    wxSendMsgService.sendApproveRejectTipInfo(tmp, check_type);
                    //修改待办状态
                    updateBacklog.setDbstatus("6");
                    updateBacklog.setBmmc(ConvertDeptToPath(problemObj.getString("bmmc")));
                    updateBacklog.setDbman("-1");
                } else {
                    if (problemObj.getString("problem_type").equals("0")) {
                        wxSendMsgService.sendApproveReviewTipInfo(ids, 0);
                        updateBacklog.setDbstatus("4");
                    } else if (problemObj.getString("problem_type").equals("1")) {
                        wxSendMsgService.lastReviewTipInfo(ids, 1);
                        updateBacklog.setDbstatus("5");
                    }
                    updateBacklog.setBmmc(bmmc);
                    updateBacklog.setDbman(idList.toString());
                }
            }
            if (op.equals("approve_reiview")) {
                int approve_result = Integer.parseInt(opObj.getString("approve_result"));
                if (approve_result == 0) {
                    ProblemFeedbackEntity fbEn = feedbackService.getProblemFeedbackEntity(problemObj.getString("id"));
                    List<String> tmp = new ArrayList<>();
                    tmp.add(fbEn.getFeedback_userid());
                    wxSendMsgService.sendApproveRejectTipInfo(tmp, check_type);
                    //修改待办状态
                    updateBacklog.setDbstatus("6");
                    updateBacklog.setBmmc(ConvertDeptToPath(problemObj.getString("bmmc")));
                    updateBacklog.setDbman("-1");
                }else {
                    if (problemObj.getString("problem_type").equals("0")) {
                        wxSendMsgService.sendApproveReviewTipInfo(ids, 0);
                        updateBacklog.setDbstatus("4");
                    } else if (problemObj.getString("problem_type").equals("1")) {
                        wxSendMsgService.lastReviewTipInfo(ids, 1);
                        updateBacklog.setDbstatus("5");
                    }
                    updateBacklog.setBmmc(bmmc);
                    updateBacklog.setDbman(idList.toString());
                }
            }
            if (op.equals("review")) {
                ProblemFeedbackEntity fbEn = feedbackService.getProblemFeedbackEntity(problemObj.getString("id"));
                List<String> tmp = new ArrayList<>();
                tmp.add(fbEn.getFeedback_userid());
                int approve_result = Integer.parseInt(opObj.getString("approve_result"));
                if (approve_result == 0) {
                    wxSendMsgService.sendApproveRejectTipInfo(tmp, check_type);
                    //修改待办状态
                    updateBacklog.setDbstatus("-100");
                } else {
                    wxSendMsgService.feedbackReviewTipInfo(tmp, Integer.parseInt(opObj.getString("approve_result")));
                    //修改待办状态
                    updateBacklog.setDbstatus("0");
                    updateBacklog.setBmmc(problemObj.getString("khbm"));
                    updateBacklog.setDbman("-1");
                }
            }
            if (!StringUtils.isEmpty(updateBacklog.getDbstatus())) {
                Map<String, Object> map = new HashMap<>();
                map.put("type", "0");
                map.put("problem_id", problemObj.getString("id"));
                List<BacklogEntity> dbList = daiBanService.selectdb(map);
                if (dbList.size() > 0) {
                    if (updateBacklog.getDbstatus().equals("-100")) {
                        daiBanService.deleteBacklog(dbList.get(0).getId());
                    } else {
                        updateBacklog.setBmmc(updateBacklog.getBmmc().replaceAll("超高压输电公司/超高压输电公司/","超高压输电公司/"));
                        updateBacklog.setId(dbList.get(0).getId());
                        updateBacklog.setNewTime("1");
                        daiBanService.updateBacklog(updateBacklog);
                    }
                }
            }
        }catch (Exception ex){
            log.error("打分实时通知异常:"+ex.getMessage());
        }
    }

    /**
     *
     * @param req
     * @return
     */
    @GetMapping("/getSubDeptStat")
    public ResultVo updateProblemInst(@RequestParam Map<String,String> req){
        String userId = req.get("userid");
        String dept = req.get("department_path");
        String check_type = req.get("problem_type");

        CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(check_type));
        if(binst == null){
            ResultVo retVo = null;
            retVo = ResultVoUtil.error("9999","当前无批次");
            return retVo;
        }

        List<GradeDeptStatEntity> retStat = new ArrayList<GradeDeptStatEntity>();

        Map<String,Object> statCondition = new HashMap<String, Object>();
        statCondition.put("batch_id",binst.getId());
        if(check_type.equals("0")){
            DepartVo subDept = wxDepartmentService.getUserSubDept(userId,dept);
            statCondition.put("grade_dept",subDept.getDepartment_path());
            retStat = problemService.getDeptGradeStatCompany(statCondition);
        }else {
            statCondition.put("assessment_dept",dept);
            retStat = problemService.getDeptGradeStatDept(statCondition);
        }


        ResultVo retVo = ResultVoUtil.success("success",retStat);
        return retVo;
    }

    /**
     * 批量处理评分问题，批量提交审核，批量审核同意，批量审核退回
     * @param jsonobj
     * @return
     */
    @PostMapping("batchUpdateProblem")
    public ResultVo batchUpdateProblem(@RequestBody JSONObject jsonobj){
        String op = jsonobj.getString("op");
        String userid = jsonobj.getString("user_id");
        String username = jsonobj.getString("user_name");
        String dept = jsonobj.getString("department_path");
        String strChkType = jsonobj.getString("check_type");
        String refuseReason = jsonobj.getString("refuse_reason");
        String batchDept = jsonobj.getString("batchDept");
        JSONArray arr = jsonobj.getJSONArray("receivers");


        CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(strChkType));
        if(binst == null){
            ResultVo retVo = null;
            retVo = ResultVoUtil.error("9999","当前无批次");
            return retVo;
        }

        if(op.equals("batchCommit")){
            //判断该部门是否全部打分完毕，否则返回错误
            int noGradeCnt = -1;
            Map<String,Object> statCondition = new HashMap<String, Object>();
            statCondition.put("assessment_dept",dept);
            statCondition.put("batch_id",binst.getId());
            statCondition.put("problem_status", 0);
            noGradeCnt = problemService.getProblemCountByStatusWithCondition(statCondition);
            if(noGradeCnt > 0){
                ResultVo retVo = null;
                retVo = ResultVoUtil.error("1011","部门所有问题评分完成才能批量提交");
                return retVo;
            }
            //获取当前人所选部门所在科室
            DepartVo subDept = wxDepartmentService.getUserSubDept(userid,dept);

            //此处必须先完成打分审核人记录，日志记录，再修改问题状态，顺序不能错，
            //处理receivers

            //批量删除审核人
            Map<String,Object> batchDelApproveCondition = new HashMap<String, Object>();
            batchDelApproveCondition.put("approve_type",0);
            batchDelApproveCondition.put("batch_id",binst.getId());
            batchDelApproveCondition.put("grade_dept",subDept.getDepartment_path());
            batchDelApproveCondition.put("check_userid",userid);
            batchDelApproveCondition.put("problem_status", 8);
            approveUserSelectService.batchDeleteApproveUser(batchDelApproveCondition);

            List<String> ids = new ArrayList<>();
            StringBuilder idList = new StringBuilder();
            String bmmc = "";
            //批量增加审核人
            if(arr != null && arr.size() > 0){
                for(int i=0;i<arr.size();i++){
                    Map<String,Object> batchApproveCondition = new HashMap<String, Object>();
                    batchApproveCondition.put("user_id",arr.getJSONObject(i).getString("user_id"));
                    batchApproveCondition.put("user_name",arr.getJSONObject(i).getString("user_name"));
                    batchApproveCondition.put("approve_type",0);
                    batchApproveCondition.put("batch_id",binst.getId());
                    batchApproveCondition.put("grade_dept",subDept.getDepartment_path());
                    batchApproveCondition.put("check_userid",userid);
                    batchApproveCondition.put("problem_status", 8);
                    approveUserSelectService.batchInsertApproveUser(batchApproveCondition);
                    ids.add(arr.getJSONObject(i).getString("user_id"));
                    idList.append(";").append(arr.getJSONObject(i).getString("user_id"));
                    bmmc = arr.getJSONObject(i).getString("department_displaypath");
                }
            }


            //记录日志
            Map<String,Object> batchhistoryCondition = new HashMap<String, Object>();
            batchhistoryCondition.put("create_time", new Date());
            batchhistoryCondition.put("commit", 1);
            batchhistoryCondition.put("op_event","grade");
            batchhistoryCondition.put("batch_id",binst.getId());
            batchhistoryCondition.put("grade_dept",subDept.getDepartment_path());
            batchhistoryCondition.put("check_userid",userid);
            batchhistoryCondition.put("problem_status", 8);
            objHistoryService.batchInsertObjHistory(batchhistoryCondition);

            //提交该科室所有的状态为8的问题，变更问题状态
            Map<String,Object> updateCondition = new HashMap<String, Object>();
            updateCondition.put("w_grade_dept",subDept.getDepartment_path());
            updateCondition.put("w_problem_status", 8);
            updateCondition.put("w_check_userid", userid);
            updateCondition.put("problem_status", 1);
            problemService.updateProblemInst(updateCondition);

            //待办
            Map<String, Object> daibanUpdate = new HashMap<String, Object>();
            daibanUpdate.put("problem_status", "8");
            daibanUpdate.put("grade_dept", subDept.getDepartment_path());
            daibanUpdate.put("check_type", strChkType);
            daibanUpdate.put("type", "0");
            daibanUpdate.put("score_type", "0");
            daibanUpdate.put("dbstatus", "1");
            daibanUpdate.put("dbman", idList.toString());
            daibanUpdate.put("bmmc", bmmc);
            daiBanService.updateBacklogByGradeDept(daibanUpdate);

            //消息推送
            if(ids.size() > 0){
                wxSendMsgService.sendCheckTipInfo(ids,Integer.parseInt(strChkType));
            }

        }

        if(op.equals("batchAgree")){

            //批量记录日志
            Map<String,Object> batchhistoryCondition = new HashMap<String, Object>();
            batchhistoryCondition.put("create_time",new Date());

            batchhistoryCondition.put("user_id",userid);
            batchhistoryCondition.put("approve_type",0);
            batchhistoryCondition.put("batch_id",binst.getId());
            batchhistoryCondition.put("grade_dept",dept);
            batchhistoryCondition.put("old_problem_status",1);
            if(binst.getCheck_type() == 0){
                batchhistoryCondition.put("company",batchDept);
            }else {
                batchhistoryCondition.put("dept",batchDept);
            }

            if(binst.getCurrent_stage() == 0){
                batchhistoryCondition.put("approve","评分审核，审核通过");
            }
            if(binst.getCurrent_stage() == 2){
                batchhistoryCondition.put("approve","最终评分审核，审核通过，问题结束");
            }
            objHistoryService.batchInsertApproveObjHistory(batchhistoryCondition);

            //提交该科室或部门所有的状态为1的问题，改为2
            //DepartVo subDept = wxDepartmentService.getUserSubDept(userid,dept);
            Map<String,Object> updateCondition = new HashMap<String, Object>();
            if(binst.getCurrent_stage() == 0) {
                updateCondition.put("problem_status", 2);
            }
            if(binst.getCurrent_stage() == 2) {
                updateCondition.put("problem_status", 7);
            }
            updateCondition.put("old_problem_status",1);
            updateCondition.put("user_id",userid);
            updateCondition.put("batch_id",binst.getId());
            updateCondition.put("grade_dept",dept);
            if(binst.getCheck_type() == 0){
                updateCondition.put("company",batchDept);
            }else {
                updateCondition.put("dept",batchDept);
            }
            problemService.batchApproveProblem(updateCondition);

        }

        if(op.equals("batchReject")){

            //批量记录日志
            Map<String,Object> batchhistoryCondition = new HashMap<String, Object>();
            batchhistoryCondition.put("create_time",new Date());
            //batchhistoryCondition.put("reject",refuseReason);
            batchhistoryCondition.put("user_id",userid);
            batchhistoryCondition.put("approve_type",0);
            batchhistoryCondition.put("batch_id",binst.getId());
            batchhistoryCondition.put("grade_dept",dept);
            batchhistoryCondition.put("old_problem_status",1);
            if(binst.getCheck_type() == 0){
                batchhistoryCondition.put("company",batchDept);
            }else {
                batchhistoryCondition.put("dept",batchDept);
            }

            if(binst.getCurrent_stage() == 0){
                batchhistoryCondition.put("reject","评分审核，审核拒绝，拒绝原因："+refuseReason);
            }
            if(binst.getCurrent_stage() == 2){
                batchhistoryCondition.put("reject","最终评分审核，审核拒绝，拒绝原因："+refuseReason);
            }

            objHistoryService.batchInsertApproveObjHistory(batchhistoryCondition);

            //提交该科室或部门所有的状态为1的问题，改为2
            //DepartVo subDept = wxDepartmentService.getUserSubDept(userid,dept);
            Map<String,Object> updateCondition = new HashMap<String, Object>();
            updateCondition.put("problem_status",0);
            updateCondition.put("old_problem_status",1);
            updateCondition.put("user_id",userid);
            updateCondition.put("batch_id",binst.getId());
            updateCondition.put("grade_dept",dept);
            if(binst.getCheck_type() == 0){
                updateCondition.put("company",batchDept);
            }else {
                updateCondition.put("dept",batchDept);
            }
            problemService.batchApproveProblem(updateCondition);


            //统一发条消息
        }

        ResultVo retVo = ResultVoUtil.success("success",null);
        return retVo;
    }

    /**
     * 评分审核时查看总体打分和待审核情况
     * @param req
     * @return
     */
    @GetMapping("getApproveStat")
    public ResultVo getApproveStat(@RequestParam Map<String,String> req){
        String userid = req.get("user_id");
        String dept = req.get("department_path");
        String strChkType = req.get("check_type");

        CheckBatchInst binst = checkBatchService.getCurrentCheckBachInst(Integer.parseInt(strChkType));
        if(binst == null){
            ResultVo retVo = null;
            retVo = ResultVoUtil.error("9999","当前无批次");
            return retVo;
        }

        JSONObject retObj = new JSONObject();

        //总体情况统计
        JSONObject stat_total = new JSONObject();
        int totalCnt = 0;
        int forApprove = 0; //待审核数量
        int approved = 0;//已审核
        Map<String,Object> statCondition = new HashMap<String, Object>();
        statCondition.put("assessment_dept",dept);
        statCondition.put("batch_id",binst.getId());

        List<StatusCountEntity> statls = problemService.getProblemStatusCountByWithCondition(statCondition);
        if(statls != null && statls.size() > 0){
            for(int i=0;i<statls.size();i++){
                totalCnt += statls.get(i).getCount();
                if(statls.get(i).getStatus() == 1){
                    forApprove = statls.get(i).getCount();
                }
                if(statls.get(i).getStatus() != 0 && statls.get(i).getStatus() != 8 && statls.get(i).getStatus() != 1){
                    approved += statls.get(i).getCount();
                }
            }
        }
        stat_total.put("total",totalCnt);
        stat_total.put("approve",forApprove);
        stat_total.put("accept",approved);
        retObj.put("stat_total",stat_total);

        //被考核的每个局或部门的情况统计
        List<GradeDeptStatEntity> retStat = new ArrayList<GradeDeptStatEntity>();

        Map<String,Object> deptStatCondition = new HashMap<String, Object>();
        deptStatCondition.put("batch_id",binst.getId());
        List<Integer> statusls = new ArrayList<Integer>();
        statusls.add(1);
        statusls.add(2);
        statusls.add(3);
        statusls.add(4);
        statusls.add(5);
        statusls.add(6);
        statusls.add(7);
        deptStatCondition.put("problem_status_in",statusls);

        JSONArray arr = new JSONArray();

        if(strChkType.equals("0")){
            DepartVo subDept = wxDepartmentService.getUserSubDept(userid,dept);
            deptStatCondition.put("assessment_dept",dept);
            deptStatCondition.put("problem_type",0);
            retStat = problemService.selectApproveProblemStatusNumStat(deptStatCondition);

            //公司对单位打分，提供各科室的打分情况
            if(retStat != null && retStat.size() > 0){
                for(int i=0;i<retStat.size();i++){
                    Map<String,Object> subDeptStatCondition = new HashMap<String, Object>();
                    subDeptStatCondition.put("assessment_dept",dept);
                    subDeptStatCondition.put("company",retStat.get(i).getDepartment_path());
                    subDeptStatCondition.put("problem_status_in",statusls);
                    List<GradeDeptStatEntity> subDeptls = problemService.getDeptGradeStatSubDept(subDeptStatCondition);
                    JSONObject statItem = (JSONObject) JSONObject.toJSON(retStat.get(i));
                    JSONArray subDeptStat = new JSONArray();
                    if(subDeptls != null && subDeptls.size() > 0){
                        for(int j=0;j<subDeptls.size();j++){
                            if(subDeptls.get(j).getDepartment_path().startsWith(dept)) {
                                subDeptStat.add(subDeptls.get(j));
                            }
                        }
                    }
                    statItem.put("subDeptStat",subDeptStat);
                    arr.add(statItem);
                }
            }


        }else {
            deptStatCondition.put("assessment_dept",dept);
            deptStatCondition.put("problem_type",1);
            retStat = problemService.selectApproveProblemStatusNumStat(deptStatCondition);
            if(retStat != null && retStat.size() > 0){
                for(int i=0;i<retStat.size();i++){
                    arr.add(retStat.get(i));
                }
            }
        }
        retObj.put("stat_list",arr);

        ResultVo retVo = ResultVoUtil.success("success",retObj);
        return retVo;
    }

    /**
     * 获取某个单位各个科室评分情况
     * @param req
     * @return
     */
    @GetMapping("getSubDepGradeStat")
    public ResultVo getSubDepGradeStat(@RequestParam Map<String,String> req){
        String userid = req.get("user_id");
        String assess_dept = req.get("department_path");
        String dept = req.get("dept");
        String strChkType = req.get("check_type");



        ResultVo retVo = ResultVoUtil.success("success",null);
        return retVo;
    }
}


